<template>
  <div class="role-select">
    <div class="header">
      <button class="back-btn" @click="goBack">← 返回</button>
      <h2>选择角色</h2>
      <div class="header-right"></div>
    </div>

    <div class="content">
      <div v-if="loading" class="loading">加载中...</div>

      <div v-else>
        <div class="script-info" v-if="script">
          <h3>{{ script.name }}</h3>
          <p>{{ script.description }}</p>
        </div>

        <div class="roles-section">
          <h3>可选角色</h3>
          <div class="roles-list">
            <div v-for="role in availableRoles" :key="role.id" class="role-card" :class="{ selected: selectedRole?.id === role.id, taken: role.taken }" @click="selectRole(role)">
              <div class="role-header">
                <h4>{{ role.name }}</h4>
                <span v-if="role.taken" class="taken-badge">已选</span>
              </div>
              <p class="role-description">{{ role.description }}</p>
              <div class="role-info">
                <span class="info-tag">性别: {{ role.gender }}</span>
                <span class="info-tag">年龄: {{ role.age }}</span>
              </div>
            </div>
          </div>
        </div>

        <div class="actions">
          <button class="btn btn-primary" @click="confirmSelection" :disabled="!selectedRole || selectedRole.taken">确认选择</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from "vue";
import { useRouter, useRoute } from "vue-router";
import axios from "axios";

export default {
  name: "RoleSelect",
  setup() {
    const router = useRouter();
    const route = useRoute();
    const roomId = route.params.id;

    const loading = ref(true);
    const script = ref(null);
    const roles = ref([]);
    const selectedRole = ref(null);

    const loadRoles = async () => {
      try {
        loading.value = true;
        const response = await axios.get(`/api/rooms/${roomId}/roles`);
        if (response.data.success) {
          script.value = response.data.script;
          roles.value = response.data.roles;
        }
      } catch (error) {
        console.error("加载角色列表失败:", error);
      } finally {
        loading.value = false;
      }
    };

    const selectRole = (role) => {
      if (!role.taken) {
        selectedRole.value = role;
      }
    };

    const confirmSelection = async () => {
      if (!selectedRole.value || selectedRole.value.taken) {
        return;
      }

      try {
        const response = await axios.post(`/api/rooms/${roomId}/select-role`, {
          roleId: selectedRole.value.id,
        });

        if (response.data.success) {
          alert("角色选择成功！");
          router.push(`/room/${roomId}`);
        } else {
          alert(response.data.message || "选择角色失败");
        }
      } catch (error) {
        console.error("选择角色失败:", error);
        alert("选择角色失败，请稍后重试");
      }
    };

    const goBack = () => {
      router.push(`/room/${roomId}`);
    };

    onMounted(() => {
      loadRoles();
    });

    return {
      loading,
      script,
      availableRoles: roles,
      selectedRole,
      selectRole,
      confirmSelection,
      goBack,
    };
  },
};
</script>

<style scoped>
.role-select {
  min-height: 100vh;
  background: #f5f5f5;
}

.header {
  background: white;
  padding: 15px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  position: sticky;
  top: 0;
  z-index: 100;
}

.header h2 {
  font-size: 20px;
  font-weight: 600;
}

.back-btn {
  background: none;
  border: none;
  color: #667eea;
  font-size: 16px;
  cursor: pointer;
  padding: 5px 10px;
}

.header-right {
  width: 60px;
}

.content {
  padding: 20px;
}

.loading {
  text-align: center;
  padding: 60px 20px;
  color: #999;
}

.script-info {
  background: white;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.script-info h3 {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 10px;
}

.script-info p {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
}

.roles-section {
  margin-bottom: 30px;
}

.roles-section h3 {
  font-size: 18px;
  font-weight: 600;
  margin-bottom: 15px;
  padding: 0 5px;
}

.roles-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.role-card {
  background: white;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  cursor: pointer;
  transition: all 0.3s;
  border: 2px solid transparent;
}

.role-card:active {
  transform: scale(0.98);
}

.role-card.selected {
  border-color: #667eea;
  background: #f0f4ff;
}

.role-card.taken {
  opacity: 0.6;
  cursor: not-allowed;
}

.role-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.role-header h4 {
  font-size: 18px;
  font-weight: 600;
}

.taken-badge {
  padding: 4px 12px;
  background: #ffebee;
  color: #c62828;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
}

.role-description {
  font-size: 14px;
  color: #666;
  line-height: 1.6;
  margin-bottom: 15px;
}

.role-info {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.info-tag {
  padding: 4px 10px;
  background: #f0f0f0;
  border-radius: 8px;
  font-size: 12px;
  color: #666;
}

.actions {
  position: sticky;
  bottom: 0;
  padding: 20px 0;
  background: #f5f5f5;
}

.btn {
  width: 100%;
  padding: 16px 32px;
  font-size: 16px;
  border: none;
  border-radius: 12px;
  cursor: pointer;
  font-weight: 600;
  transition: all 0.3s;
}

.btn-primary {
  background: #667eea;
  color: white;
}

.btn-primary:active:not(:disabled) {
  background: #5568d3;
  transform: scale(0.98);
}

.btn-primary:disabled {
  background: #ccc;
  cursor: not-allowed;
}
</style>

